package com.jack.vipmis.vip.api.web.controller;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jack.vipmis.common.web.controller.BaseController;
import com.jack.vipmis.vip.api.util.MsgUtil;
import com.jack.vipmis.vip.feedback.entity.FeedBack;
import com.jack.vipmis.vip.feedback.service.FeedBackService;

/**
 * 反馈信息
 * 
 * @author JackLiang
 * @version 1.0
 * @date 2014-7-9
 */
@Controller()
@RequestMapping(value = "api/feedBack")
public class FeedBackApiController extends BaseController<FeedBack, Long> {

	private static final Log	LOG	= LogFactory.getLog(FeedBackApiController.class);

	@Autowired
	private FeedBackService		feedBackService;

	/**
	 * 根据ID查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "find", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> find(@RequestParam("id") long id) {
		try {
			FeedBack f = feedBackService.findOne(id);
			return MsgUtil.doneOk(f);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 提交反馈信息接口通过json格式提交数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> add(@RequestParam("phone") String phone, @RequestParam("text") String text) {
		try {
			FeedBack m = new FeedBack();
			m.setTelephone(phone);
			m.setFbContent(text);
			FeedBack fb = feedBackService.save(m);
			if (fb != null) {
				return MsgUtil.doneOk(m);
			}
			return MsgUtil.doneMsg(MsgUtil.REQUEST_ERROR_DATA, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findPage(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize) {
		try {
			Page<FeedBack> result = feedBackService.findPage(page, pageSize);
			return MsgUtil.doneOk(result.getContent());

		} catch (Exception e) {
			if (LOG.isErrorEnabled()) {
				LOG.error(e.getMessage());
			}
		}
		return MsgUtil.doneFail();
	}
}
